package com.jfinal.enjoy

import com.intellij.lexer.Lexer
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
import com.intellij.openapi.editor.colors.TextAttributesKey
import com.intellij.openapi.fileTypes.SyntaxHighlighter
import com.intellij.psi.tree.IElementType
import com.jfinal.enjoy.psi.EnjoyTypes

class EnjoySyntaxHighlighter : SyntaxHighlighter {

    companion object {
        val BLOCK_COMMENT = TextAttributesKey.createTextAttributesKey(
            "ENJOY_BLOCK_COMMENT", DefaultLanguageHighlighterColors.BLOCK_COMMENT
        )
        val KEYWORD = TextAttributesKey.createTextAttributesKey(
            "ENJOY_KEYWORD", DefaultLanguageHighlighterColors.KEYWORD
        )

        val IDENTIFIER = TextAttributesKey.createTextAttributesKey(
            "ENJOY_IDENTIFIER", DefaultLanguageHighlighterColors.IDENTIFIER
        )

        val STRING = TextAttributesKey.createTextAttributesKey(
            "ENJOY_STRING", DefaultLanguageHighlighterColors.STRING
        )

        val NUMBER = TextAttributesKey.createTextAttributesKey(
            "ENJOY_NUMBER", DefaultLanguageHighlighterColors.NUMBER
        )

        val COMMENT = TextAttributesKey.createTextAttributesKey(
            "ENJOY_COMMENT", DefaultLanguageHighlighterColors.LINE_COMMENT
        )

        val OPERATOR = TextAttributesKey.createTextAttributesKey(
            "ENJOY_OPERATOR", DefaultLanguageHighlighterColors.OPERATION_SIGN
        )

        val BAD_CHARACTER = TextAttributesKey.createTextAttributesKey(
            "ENJOY_BAD_CHARACTER", DefaultLanguageHighlighterColors.INVALID_STRING_ESCAPE
        )
        val OUTPUT = TextAttributesKey.createTextAttributesKey(
            "ENJOY_OUTPUT", DefaultLanguageHighlighterColors.KEYWORD
        )
        val JAVA_EXPR = TextAttributesKey.createTextAttributesKey(
            "ENJOY_JAVA_EXPR", DefaultLanguageHighlighterColors.KEYWORD
        )

        val PARENTHESIS = TextAttributesKey.createTextAttributesKey(
            "ENJOY_PARENTHESIS", DefaultLanguageHighlighterColors.PARENTHESES
        )
        val OUTPUT_HASH = TextAttributesKey.createTextAttributesKey(
            "ENJOY_OUTPUT_HASH", DefaultLanguageHighlighterColors.KEYWORD
        )
    }

    override fun getHighlightingLexer(): Lexer {
        // 返回你写的 EnjoyLexerAdapter 或 Lexer 实现
        return EnjoyLexerAdapter()
    }

    override fun getTokenHighlights(tokenType: IElementType): Array<TextAttributesKey> {
//        println("___$tokenType")
        return when (tokenType) {
            // 关键字类型 token，比如 IF, ELSE, FOR
            EnjoyTypes.IF,
            EnjoyTypes.ELSE, EnjoyTypes.ELSEIF, EnjoyTypes.FOR,
            EnjoyTypes.SET, EnjoyTypes.DEFINE, EnjoyTypes.INCLUDE, EnjoyTypes.SWITCH,
            EnjoyTypes.CASE, EnjoyTypes.DEFAULT, EnjoyTypes.END -> arrayOf(KEYWORD)
//            EnjoyTypes.OUTPUT_START -> arrayOf(OUTPUT)
            EnjoyTypes.JAVA_EXPR -> arrayOf(JAVA_EXPR)
            // 字符串文本
            EnjoyTypes.STRING_LITERAL -> arrayOf(STRING)

//            EnjoyTypes.OUTPUT_HASH -> arrayOf(OUTPUT)
            // 数字
            EnjoyTypes.NUMBER -> arrayOf(NUMBER)

//            EnjoyTypes.LPAREN, EnjoyTypes.RPAREN -> arrayOf(PARENTHESIS)

            // 注释
            EnjoyTypes.LINE_COMMENT_CONTENT, EnjoyTypes.BLOCK_COMMENT_CONTENT -> arrayOf(COMMENT)

            // 操作符

//            EnjoyTypes.COMPARE_OP -> arrayOf(OPERATOR)

            // 标识符
//            EnjoyTypes.IDENT -> arrayOf(IDENTIFIER)

            // 其他
            else -> TextAttributesKey.EMPTY_ARRAY
        }
    }
}
